<?php
/**
 * Created by PhpStorm.
 * User: 王泽锋
 * Date: 2018/03/02
 * Time: 13:15
 */
namespace app\admin\controller;

use app\admin\model\Shopping_commodity as Shopping_commodityModel;
/**
 * Class shopping
 * @package 商品管理   控制器
 */
class Shopping extends Admin {
    /**
     * 商品主页
     */
    public function index(){
        $map = array(
            'status' => array('eq',0),
        );
        $list = $this->lists('Shopping_commodity',$map);
        int_to_string($list,array(
            'status' => array(0 =>"已发布"),
        ));

        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 商品添加
     */
    public function add(){
        if(IS_POST) {
            $data = input('post.');
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            if(empty($data['id'])){
                unset($data['id']);
            }
            $shopping_commodityModel = new Shopping_commodityModel();
            $info = $shopping_commodityModel->validate('Commodity')->save($data);
            if($info) {
                return $this->success("添加成功",Url('Shopping/index'));
            }else{
                return $this->error($shopping_commodityModel->getError());
            }
        }else{
            $this->assign('msg','');
            return $this->fetch('edit');
        }
    }

    /**
     * 商品修改
     */
    public function edit(){
        if(IS_POST) {
            $data = input('post.');
            $shopping_commodityModel = new Shopping_commodityModel();
            $info = $shopping_commodityModel->validate('Commodity')->save($data,['id'=>input('id')]);
            if($info){
                return $this->success("修改成功",Url("Shopping/index"));
            }else{
                return $this->get_update_error_msg($shopping_commodityModel->getError());
            }
        }else{
            $id = input('id');
            $msg = Shopping_commodityModel::get($id);
            $this->assign('msg',$msg);
            return $this->fetch();
        }
    }

    /**
     * 商品删除功能
     */
    public function del(){
        $id = input('id');
        $data['status'] = '-1';
        $info = Shopping_commodityModel::where('id',$id)->update($data);
        if($info) {
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }

    }

}